package cn.iocoder.yudao.module.enterprise.service.enterpriseCurriculumVitae;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.enterprise.controller.admin.enterpriseCurriculumVitae.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeDO;

import cn.iocoder.yudao.module.enterprise.dal.dataobject.studentEducationalExperience.EnterpriseStudentEducationalExperienceDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.studentEducationalExperience.EnterpriseStudentEducationalExperienceMapper;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.CurriculumVitaeSendRecordApi;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.dto.CurriculumVitaeSendRecordRespDTO;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.dto.CurriculumVitaeSendRecordUpdateReqDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.STUDENT_CURRICULUM_VITAE_NOT_EXISTS;

/**
 * 用户简历 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class EnterpriseCurriculumVitaeServiceImpl implements EnterpriseCurriculumVitaeService {

    @Resource
    private EnterpriseCurriculumVitaeMapper curriculumVitaeMapper;
    @Resource
    private CurriculumVitaeSendRecordApi curriculumVitaeSendRecordApi;
    @Resource
    private EnterpriseStudentEducationalExperienceMapper enterpriseStudentEducationalExperienceMapper;
    @Override
    public Long createCurriculumVitae(EnterpriseCurriculumVitaeCreateReqVO createReqVO) {
        // 插入
        EnterpriseCurriculumVitaeDO curriculumVitae = EnterpriseCurriculumVitaeConvert.INSTANCE.convert(createReqVO);
        curriculumVitaeMapper.insert(curriculumVitae);
        // 返回
        return curriculumVitae.getId();
    }

    @Override
    public void updateCurriculumVitae(EnterpriseCurriculumVitaeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCurriculumVitaeExists(updateReqVO.getId());
        // 更新
        EnterpriseCurriculumVitaeDO updateObj = EnterpriseCurriculumVitaeConvert.INSTANCE.convert(updateReqVO);
        curriculumVitaeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCurriculumVitae(Long id) {
        // 校验存在
        this.validateCurriculumVitaeExists(id);
        // 删除
        curriculumVitaeMapper.deleteById(id);
    }

    private void validateCurriculumVitaeExists(Long id) {
        if (curriculumVitaeMapper.selectById(id) == null) {
            throw exception(STUDENT_CURRICULUM_VITAE_NOT_EXISTS);
        }
    }

    @Override
    @TenantIgnore
    public EnterpriseCurriculumVitaeDO getCurriculumVitae(Long id,Long recordId) {
        CurriculumVitaeSendRecordRespDTO curriculumVitaeSendRecordRespDTO=curriculumVitaeSendRecordApi.getCurriculumVitaeSend(recordId,false);
        String status=curriculumVitaeSendRecordRespDTO.getStatus();

        if(status!=null&&status.equals("0")) {
            CurriculumVitaeSendRecordUpdateReqDTO curriculumVitaeSendRecordUpdateReqDTO = new CurriculumVitaeSendRecordUpdateReqDTO();
            curriculumVitaeSendRecordUpdateReqDTO.setStatus("1");
            curriculumVitaeSendRecordUpdateReqDTO.setId(recordId);
            curriculumVitaeSendRecordApi.updateCurriculumVitaeSend(curriculumVitaeSendRecordUpdateReqDTO);
        }
        EnterpriseCurriculumVitaeDO  enterpriseCurriculumVitaeDO=curriculumVitaeMapper.selectById(id);
        List<EnterpriseStudentEducationalExperienceDO> enterpriseStudentEducationalExperienceDOS= enterpriseStudentEducationalExperienceMapper.selectListByStudentId(id);
        enterpriseCurriculumVitaeDO.setEducationalExperience(enterpriseStudentEducationalExperienceDOS);
        return enterpriseCurriculumVitaeDO;
    }

    @Override
    @TenantIgnore
    public EnterpriseCurriculumVitaeDO getCurriculumVitae(Long id) {
        EnterpriseCurriculumVitaeDO enterpriseCurriculumVitaeDO=curriculumVitaeMapper.selectById(id);
        if(enterpriseCurriculumVitaeDO!=null) {
            //教育经历
            List<EnterpriseStudentEducationalExperienceDO> enterpriseStudentEducationalExperienceDOS = enterpriseStudentEducationalExperienceMapper.selectListByStudentId(id);
            enterpriseCurriculumVitaeDO.setEducationalExperience(enterpriseStudentEducationalExperienceDOS);
        }
        return enterpriseCurriculumVitaeDO;
    }


    @Override
    public List<EnterpriseCurriculumVitaeDO> getCurriculumVitaeList(Collection<Long> ids) {
        return curriculumVitaeMapper.selectBatchIds(ids);
    }

    @Override
    @TenantIgnore
    public PageResult<EnterpriseCurriculumVitaeDO> getCurriculumVitaePage(EnterpriseCurriculumVitaePageReqVO pageReqVO) {
        Integer total = curriculumVitaeMapper.countCurriculumVitae(pageReqVO);
        if (total <= 0) {
            return PageResult.empty();
        }
        List<EnterpriseCurriculumVitaeDO> companyUserPage = curriculumVitaeMapper.getCurriculumVitaePage(pageReqVO);
        return PageResult.build(companyUserPage, total);
    }

    @Override
    @TenantIgnore
    public PageResult<EnterpriseCurriculumVitaeDO> getEnterpriseCurriculumVitaePage(EnterpriseCurriculumVitaePageReqVO pageReqVO) {
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        pageReqVO.setLoginUserId(loginUserId);
        Integer total = curriculumVitaeMapper.countEnterpriseCurriculumVitae (pageReqVO);
        if (total <= 0) {
            return PageResult.empty();
        }
        List<EnterpriseCurriculumVitaeDO> companyUserPage = curriculumVitaeMapper.getEnterpriseCurriculumVitaePageReqVO(pageReqVO);
        if(CollectionUtils.isNotEmpty(companyUserPage)){
            companyUserPage.stream().forEach(m->{
                List<EnterpriseStudentEducationalExperienceDO> educationalExperience = curriculumVitaeMapper.getStudentEducationalExperience(m.getId());
                m.setEducationalExperience(educationalExperience);
            });
        }

        return PageResult.build(companyUserPage, total);
    }

    @Override
    @TenantIgnore
    public List<EnterpriseCurriculumVitaeDO> getCurriculumVitaeList(EnterpriseCurriculumVitaeExportReqVO exportReqVO) {
        return curriculumVitaeMapper.selectList(exportReqVO);
    }

}
